using System;
using System.IO;

namespace dotnet
{
    public class Utils
    {
        public static string PathToWin(string path)
        {
            return path.Replace("/", @"\");
        }

        public static string PathToLinx(string path)
        {
            return path.Replace(@"\", "/");
        }

        public static string GetSlash()
        {
            switch(Utils.GetPlatform())
            {
                case Utils.Platform.Windows:
                    return @"\";
                case Utils.Platform.Linux:
                    return "/";
                default:
                    return "/";
            }
        }

        public static string MakeCurCfgPath()
        {
            return Directory.GetCurrentDirectory() + GetSlash() + Configs.ConfigDir;
        }

        public static string GetRelativePath(string root, string path)
        {
            if (path.Length > root.Length + 1)
            {
                if (root.Equals(path.Substring(0, root.Length)))
                {
                    return path.Substring(root.Length + 1);
                }
            }
            return null;
        }

        public enum Platform
        {
            Unknow = 0,
            Windows,
            Linux
        }
        public static Platform GetPlatform()
        {
            return Platform.Windows;
        }

        public static string FormatPath(string path)
        {
            switch(Utils.GetPlatform())
            {
                case Utils.Platform.Windows:
                    return path.Replace('/', '\\');
                case Utils.Platform.Linux:
                    return path.Replace('\\', '/');
                default:
                    return path.Replace('\\', '/');
            }
        } 

        public static void ErrorLog(string msg)
        {
            System.Console.WriteLine("\n[Error]:");
            System.Console.WriteLine("\t" + msg);
        }

        public static void SuccessLog(string msg)
        {
            System.Console.WriteLine("\n[Successful]:");
            System.Console.WriteLine("\t" + msg);
        }

        public static void ErrorFormat(string msg)
        {
            System.Console.WriteLine("\n[Format Error]:");
            System.Console.WriteLine("\t" + msg);
        }

        public static void MessageLog(string msg)
        {
            System.Console.WriteLine("\n[Message]:");
            System.Console.WriteLine("\t" + msg);
        }

        public static string GetTopCfgPath()
        {
            string curpath = Directory.GetCurrentDirectory();
            string rootpath = Directory.GetDirectoryRoot(curpath);
            for (DirectoryInfo dirinfo = new DirectoryInfo(curpath); !rootpath.Equals(dirinfo.FullName); dirinfo = dirinfo.Parent)
            {
                foreach (var dir in dirinfo.GetDirectories())
                {
                    if (dir.Name.Equals(Configs.ConfigDir))
                    {
                        return dir.FullName;
                    }
                }
            }
            return null;
        }

        public static int TimeCmp(DateTime source, DateTime target)
        {
            DateTime ntar = new DateTime(target.Year, target.Month, target.Day, target.Hour, target.Minute, target.Second, source.Millisecond);
            return source.CompareTo(ntar);
        }

        public static string GenerateRemotePath(string localbasepath, string serverbasepath, string localpath)
        {
            string remote = localpath.Substring(localbasepath.Length).Replace('\\', '/');
            return serverbasepath + remote;
        }

        public static string GenerateLocalPath(string localbasepath, string serverbasepath, string remotepath)
        {
            string local = remotepath.Substring(serverbasepath.Length).Replace("/", Utils.GetSlash());
            return localbasepath + local;
        }
        
    }
}